<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CIHeadStart Number Helpers
 *
 * @author Casey McLaughlin
 * @package CIHeadStart
 * @link http://code.google.com/p/ciheadstart
 */
 
 // --------------------------------------------------------------------

/**
 * Returns the median of a list of numbers.
 *
 * @param int $num,...
 * @return <type>
 */
function median()
{
    $args = func_get_args();

    switch(func_num_args())
    {
        case 0:
            trigger_error('median() requires at least one parameter',E_USER_WARNING);
            return false;
            break;

        case 1:
            $args = array_pop($args);
            // fallthrough

        default:
            if( ! is_array($args)) {
                trigger_error('median() requires a list of numbers to operate on or an array of numbers',E_USER_NOTICE);
                return false;
            }

            sort($args);

            $n = count($args);
            $h = intval($n / 2);

            if($n % 2 == 0) {
                $median = ($args[$h] + $args[$h-1]) / 2;
            } else {
                $median = $args[$h];
            }

            break;
    }

    return $median;
}

/* End of file MY_number_helper.php */
/* Location: ./app/helpers/MY_number_helper.php */